<?php
namespace app\index\controller;

use app\common\controller\Base;
use app\common\model\ArtCate;
use app\common\model\Article;
use app\common\model\Comment;
use think\Db;
use think\facade\Request;
use think\facade\Session;  //导入公共控制器

class Index extends Base
{




    public function index()
    {
        //全局查询条件
        $map=[];
        //条件1
        $map[]=['status','=',1];
        $keyword=Request::param('keyword');
        if(!empty($keyword)){
            $map[]=['title','like','%'.$keyword.'%']; //条件2
        }
        $id=Request::param('cate_id');
        if(isset($id)){
            $res=ArtCate::get($id);
            //条件3
            $map[]=['cate_id','=',$id];
            $art=Db::table('zh_article')->where($map)
                                               ->order('create_time','desc')
                                               ->paginate(3);

            $this->assign('cateName',$res['name']);
        }else{
            $art=Db::table('zh_article')->where($map)
                                               ->order('create_time','desc')
                                               ->paginate(3);
            $this->assign('cateName','首页');
        }
        $this->assign('empty','没有文章');
        $this->assign('art',$art);
        return $this->fetch();
    }



    //添加文章界面
    public function insert()
    {
        //1\登录才允许发布文章
        $this->isLogin();
        //2\设置页面标题
        $this->assign('title','发布文章');
        //3、获取一下栏目的信息
        $cateList=ArtCate::all();
        if(count($cateList)>0){
            $this->assign('cateList',$cateList);
        }else{
            $this->error('请先添加栏目','index/index');
        }
        //f阿布界面渲染
        return $this->fetch('insert');
    }

    //保存文件
    public function save(){
        //判断提交类型
        if(Request::isPost()){
            $data=Request::post();
            $res=$this->validate($data,'app\common\validate\Article');
            if ($res!==true){
                echo "<script>alert('".$res."');window.location.href='insert';</script>";
            }else{
                //验证成功
                $file=Request::file('title_image');
                $info=$file->validate([
                    'ext'=>'jpeg,jpg,png,gif',
                ])->move('uploads/');
                if($info){
                    $data['title_image']=$info->getSaveName();
                }else{
                    $this->error($file->getError());
                }
                //将数据插入到数据表中
                if(Article::create($data)){
                    $this->success('文章发送成功','insert');
                }
            }
        }else{
            $this->error('请求类型错误');
        }
    }

    public function upload(){
        $file = request()->file('imgFile');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->move( 'uploads/');
        if($info){
            // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            $src=$info->getSaveName();
            return json_encode(['error' => 0, 'url' => '/uploads/'.$src]);
        }else{
            // 上传失败获取错误信息
            echo $file->getError();
        }
    }



    //详情页
    public function details(){
        $id=Request::param('id');
        $art=Article::get(function($query) use($id){
            $query->where('id',$id)->setInc('pv');
        } );
        $is_fav=Db::table('zh_user_fav')->where(['user_id'=>Session::get('user_id'),'article_id'=>$id])->find();
        if(is_null($is_fav)){
            $is_fav=0;
        }else{
            $is_fav=1;
        }
        $comment=Comment::where('article_id',$id)->select();
        $comment=$this->cateTree($comment);
        $this->assign('comment',$comment);
        $this->assign('is_fav',$is_fav);
        $this->assign('art',$art);
        $this->assign('title','详情页');
        return $this->fetch();
    }


    //收藏
    public function fav(){
        if(!Request::isAjax()){
            return ['status'=>-1,'msg'=>'提交方式有误'];
        }
        $user_id=Request::param('session_id');
        $article_id=Request::param('article_id');
        if(empty($user_id)){
            return ['status'=>2,'msg'=>'请登录后在收藏'];
        }
        $data=Db::table('zh_user_fav')->where(['user_id'=>$user_id,'article_id'=>$article_id])->find();
        if(is_null($data)){
            Db::table('zh_user_fav')->insert(['user_id'=>$user_id,'article_id'=>$article_id]);
            return ['status'=>1,'msg'=>'收藏成功'];
        }else{
            Db::table('zh_user_fav')->where(['user_id'=>$user_id,'article_id'=>$article_id])->delete();
            return ['status'=>0,'msg'=>'已取消'];
        }

    }


    public function saveComment(){

        if (Request::isAjax()){
            $data=Request::param();
            if($data['pid']!=0){
                $res=Comment::where('id',$data['pid'])->select();
                if($res[0]['pid']!=0){
                    $data['pid']=$res[0]['pid'];
                }
            }
            Comment::create($data,true);
            $comment=Comment::all();
            foreach ($comment as $k=>$v){
                $v['name']=\think\Db::table('zh_user')->where('id',$v['user_id'])->value('name');
                $v['reply_name']=\think\Db::table('zh_user')->where('id',$v['reply_id'])->value('name');
            }
            $comment=$this->cateTree($comment);
            return ['status'=>1,'res'=>'评论成功','comment'=>$comment];
        }
    }
}
